package co.yixiang.business.demo

import me.liuwj.ktorm.database.Database
import me.liuwj.ktorm.entity.Entity
import me.liuwj.ktorm.entity.sequenceOf
import me.liuwj.ktorm.schema.*
import java.time.LocalDate
import javax.persistence.Id
import javax.validation.constraints.Null

interface Department : Entity<Department> {

    companion object : Entity.Factory<Department>()

    val id: Int

    var name: String

    var location: String
}

object Departments : Table<Department>("") {

    val id = int("id").primaryKey().bindTo { it.id }

    val name = varchar("name").bindTo { it.name }

    val location = varchar("location").bindTo { it.location }

    val Database.departments get() = this.sequenceOf(Departments)
}

object Employees : Table<Employee>("") {

    val id = int("id").primaryKey().bindTo { it.id }
    val name = varchar("name").bindTo { it.name }
    val job = varchar("job").bindTo { it.job }
    val managerId = int("manager_id").bindTo { it.manager.id }
    val hireDate = date("hire_date").bindTo { it.hireData }
    val salary = long("salary").bindTo { it.salary }
    val departmentId = int("department_id").references(Departments) { it.department }
}

interface Employee: Entity<Employee> {

    companion object : Entity.Factory<Employee>()

    val id: Int

    var name: String

    var job: String

    var manager: Employee

    var hireData: LocalDate

    var salary: Long

    var department: Department
}

fun main() {

    val department = Department {

        name = "111"
        location = "2222"
    }

    println(department.name)
}
